import { defineStore } from "pinia";
import { Theme } from "@/stores/interface";
import piniaPersistConfig from "@/stores/helper/persist";
/**
 * @description: 设置默认颜色
 * @return {*}
 */
const setDefaultColor = (
  cacheField: "theme-primary-color" | "theme-primaryPlain-color" | "theme-type",
  defaultColor: "#3A75C5" | "#ECF5FF" | "blue-theme"
) => {
  return uni.getStorageSync(cacheField) || defaultColor;
};

export const useThemeStore = defineStore({
  id: "theme",
  state: (): Theme => ({
    theme: setDefaultColor("theme-type", "blue-theme"),
    basicColor: {
      primary: setDefaultColor("theme-primary-color", "#3A75C5"),
      primary_plain: setDefaultColor("theme-primaryPlain-color", "#ECF5FF")
    }
  }),
  actions: {
    // 设置主题
    setTheme(theme: Theme["theme"]) {
      this.theme = theme;
      this.setColor(theme);
    },
    setColor(themeType) {
      switch (themeType) {
        case "blue-theme":
          this.basicColor.primary = "#3A75C5";
          this.basicColor.primary_plain = "#ECF5FF";
          break;
        case "jade-primary":
          this.basicColor.primary = "#c04851";
          this.basicColor.primary_plain = "rgba(192, 72, 81, 0.2)";
          break;
        case "orange-primary":
          this.basicColor.primary = "#FF7052";
          this.basicColor.primary_plain = "#fef0f0";
          break;
        default:
          this.basicColor.primary = "#303133";
          this.basicColor.primary_plain = "#f1f1f1";
          break;
      }
      uni.setStorageSync("theme-primary-color", this.basicColor.primary);
      uni.setStorageSync("theme-primaryPlain-color", this.basicColor.primary_plain);

      uni.setStorageSync("theme-type", themeType);
    }
  },
  persist: piniaPersistConfig("theme")
});
